package cn.nyse.ktc_spit.controller;


import cn.nyse.ktc_spit.dao.SpitDao;
import cn.nyse.ktc_spit.dao.ThumbupDao;
import cn.nyse.ktc_spit.entity.Spit;
import cn.nyse.ktc_spit.service.SpitService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;
import wfw.cn.ktc_common.entity.PageResult;
import wfw.cn.ktc_common.entity.Result;
import wfw.cn.ktc_common.entity.ResultCode;

@RequestMapping("/spit")
@CrossOrigin
@RestController
public class SpitController {
    @Autowired
    private SpitService spitService;

    @PostMapping("")
    public Result insertSpit(@RequestBody Spit spit){
        spitService.save(spit);
        return new Result(ResultCode.SUCCESS,null);
    }

    @GetMapping("")
    public Result findAllPage(){
        return new Result(ResultCode.SUCCESS,spitService.findAllPage());
    }

    @GetMapping("/{spitId}")
    public Result findBySpitId(@PathVariable String spitId){
        return new Result(ResultCode.SUCCESS,spitService.findBySpitId(spitId));
    }

    @PutMapping("/{spitId}")
    public Result updateSpit(@PathVariable String spitId){
        spitService.updateSpit(spitId,"1002");
        return new Result(ResultCode.SUCCESS,null);
    }
    @PostMapping("/search")
    public Result searchByCondition(@RequestBody Spit spit){
        return new Result(ResultCode.SUCCESS,spitService.searchByCondition(spit));
    }

    @PostMapping("/search/{page}/{size}")
    public Result spitPage(@RequestBody Spit spit,@PathVariable("page")int page,@PathVariable("size")int size){
        if (page<1){
            page=1;
        }
        Page<Spit> spitPage = spitService.findSpitPage(page, size, spit);
        return new Result(ResultCode.SUCCESS,new PageResult<Spit>(spitPage.getTotalElements(),spitPage.getContent()));
    }

    @GetMapping("comment/{parentid}/{page}/{size}")
    public Result findSpitByParentid(@PathVariable("parentid")String parentid,@PathVariable("page")int page,@PathVariable("size")int size){
        if (page<1){
            page=1;
        }
        return new Result(ResultCode.SUCCESS,spitService.findByParentid(parentid,page,size));
    }
}
